package company.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import company.common.R;
import company.domain.Comments;
import company.domain.Educationalarticles;
import company.domain.Users;
import company.service.CommentsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
public class CommentsController {
    @Resource
    CommentsService commentsService;

    /*
     * 连表查询
     * */
    @RequestMapping("/selectComm/{page}")
    public R selectComm(@PathVariable("page") int page){
        PageHelper.startPage(page,3);
        List<Map> list=this.commentsService.selectComm();
        PageInfo<Object> pageInfo = new PageInfo<>(list);
        return new R().add("pageInfo",pageInfo);
    }

    /*
     * 用户修改
     * */
    @PostMapping("/uploadComm")
    public R uploadComm(@RequestBody Comments comments){
        this.commentsService.updateById(comments);
        return new R();
    }

    /*
     * 注册用户
     * */
    @PostMapping("/AddComm")
    public R AddComm(@RequestBody Comments comments){
        this.commentsService.save(comments);
        return new R();
    }


    /*
     * 用户删除
     * */
    @RequestMapping("/DelComm/{id}")
    public R DelComm(@PathVariable("id") int id){
        boolean remove = this.commentsService.removeById(id);
        R r=new R();
        if(remove==true){
            r.setMessage("删除成功");
        }else{
            r.setMessage("删除失败");
        }
        return r;
    }

    /*
     * 根据用户id查询
     * */
    @PostMapping("/selectCommbyid")
    public R selectCommbyid(@RequestBody Comments comments){
        Comments byId = this.commentsService.getById(comments.getCommentid());
        return new R().add("byId",byId);
    }
}
